package com.example.student.http;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;

import com.example.student.util.ToastUtils;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.HttpUrl;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;

import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2017/8/21.
 */

public class OkHttpUtils {

    private static OkHttpUtils mInstance;
    private OkHttpClient mOkHttpClient;
    private static String TAG = "OkHttpUtils";
    private OkHttpInterface myInterface;
    private static Context mContext;

    public static synchronized void initEngine() {
        mInstance = new OkHttpUtils();
    }

    public static OkHttpUtils getInstance() {
        if (mInstance == null) {
            initEngine();
        }
        return mInstance;
    }

    public static OkHttpUtils getInstance(Context context) {
//        if (mInstance == null) {
//            synchronized (OkHttpUtils.class) {
//                mInstance = new OkHttpUtils(context);
//            }
//        }
//
//        return mInstance;
        mContext = context;
        if (mInstance == null) {
            initEngine();
        }
        return mInstance;
    }

    private OkHttpUtils() {

        Log.e(TAG, "..............是否去创建");
        mOkHttpClient = new OkHttpClient();
        mOkHttpClient.setConnectTimeout(15 * 1000, TimeUnit.SECONDS);  //60*2
        mOkHttpClient.setWriteTimeout(15 * 1000, TimeUnit.SECONDS);   //60*2
        mOkHttpClient.setReadTimeout(15 * 1000, TimeUnit.SECONDS);   //60*2
    }

    private OkHttpUtils(Context context) {
        mContext = context;
        Log.e(TAG, "..............是否去创建");
        mOkHttpClient = new OkHttpClient();
        mOkHttpClient.setConnectTimeout(15 * 1000, TimeUnit.SECONDS);  //60*2
        mOkHttpClient.setWriteTimeout(15 * 1000, TimeUnit.SECONDS);   //60*2
        mOkHttpClient.setReadTimeout(15 * 1000, TimeUnit.SECONDS);   //60*2
    }

    public void setMyInterface(OkHttpInterface okHttpinter) {
        this.myInterface = okHttpinter;
    }


//    //单例模式（静态内部类）
//    private static class SingletonHolder {
//        private static final OkHttpClient mHttpClient = new OkHttpClient();
//    }
//
//    private OkHttpUtils() {
//    }
//    public static final OkHttpClient getInstance() {
//        return SingletonHolder.mHttpClient;
//    }

    /**
     * 异步get请求
     *
     * @param url
     */
    public void getAsynHttp(final OkHttpInterface inter, final String url) {

        Log.e(TAG, "////////异步get请求" + url);
        final Request request = new Request.Builder().url(url).build();
        Call call = mOkHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                inter.setHttpFailure(url, request, e);

                Log.e(TAG, "////////异步get请求onFailure");
            }

            @Override
            public void onResponse(final Response response) throws IOException {

                int code = response.code();
                String str = response.body().string();
                Log.e(TAG, "utils方法:" + str);
                if (code == 200) {
                    inter.setHttpResponse(url, str);
                }

            }
        });
    }

    /**
     * 同步get请求
     *
     * @param url
     * @return
     * @throws IOException
     */
    public Response getSyncHttp(final String url) throws IOException {
        // OkHttpClient mOkHttpClient = new OkHttpClient();

        Log.e(TAG, "////同步get请求");
        final Request request = new Request.Builder().url(url).build();
        Call call = mOkHttpClient.newCall(request);
        Response mResponse = call.execute();
        if (mResponse.isSuccessful()) {
//            currentActivity.httpResponse(url, null, mResponse.body().string());
            return mResponse;
        } else {
//            currentActivity.httpError(url, null);
        }
        return null;
    }

    /**
     * 异步post请求
     *
     * @param url
     * @param params
     * @param myInterface
     */
    public void postAsynHttp(final OkHttpInterface myInterface, final String url, final Map<String, Object> params) {
        // OkHttpClient mOkHttpClient = new OkHttpClient();
        Log.e(TAG, "////异步post请求" + url);
        FormEncodingBuilder builer = getFormBuilder(params);
        Request request = new Request.Builder().url(url)
                .post(builer.build()).build();
        Call call = mOkHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
//                currentActivity.httpError(url, params);
                ToastUtils.dismissLoadingToast();
                Log.e(TAG, "////异步post请求......onFailure");
                myInterface.setHttpFailure(url, request, e);
            }

            @Override
            public void onResponse(Response response) throws IOException {
                ToastUtils.dismissLoadingToast();
//                currentActivity.httpResponse(url, params, response.body().string())
                String str = response.body().string();

                int code = response.code();
                Log.e(TAG, "code方法:" + code);
                //成功了会是200，失败了一种会报500

                Log.e(TAG, "utils方法:" + str);

                if (code == 200) {
                    myInterface.setHttpResponse(url, str);
                } else {
//                    Looper.prepare();
//                    ToastUtils.setToastActivity((Context) myInterface,"网络请求异常，请稍后重试");
//                    Looper.loop();

                }
//
            }
        });
    }

    /**
     * 异步post请求
     *
     * @param url
     * @param params
     * @param myInterface
     */
    public void postAsynHttp(final OkHttpInterface myInterface, final String url, final Map<String, Object> params, String access_token) {
        // OkHttpClient mOkHttpClient = new OkHttpClient();
        Log.e(TAG, "////异步post请求" + url);
        FormEncodingBuilder builer = getFormBuilder(params);
        Request request = new Request.Builder().url(url)
                .post(builer.build()).build();
        Call call = mOkHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
//                currentActivity.httpError(url, params);
                ToastUtils.dismissLoadingToast();
                Log.e(TAG, "////异步post请求......onFailure");
                myInterface.setHttpFailure(url, request, e);


            }

            @Override
            public void onResponse(Response response) throws IOException {
                ToastUtils.dismissLoadingToast();
//                currentActivity.httpResponse(url, params, response.body().string())
                String str = response.body().string();
                Log.e(TAG, "onResponse方法:" + str);
                int code = response.code();
                Log.e(TAG, "code方法:" + code);
                //成功了会是200，失败了一种会报500

                Log.e(TAG, "utils方法:" + str);

                if (code == 200) {
                    myInterface.setHttpResponse(url, str);
                } else {
//                    Looper.prepare();
//                    ToastUtils.setToastActivity((Context) myInterface,"网络请求异常，请稍后重试");
//                    Looper.loop();
//                    MyApplication.netWorkErrorTips(context,code+"", (Activity)context);
                }
//
            }
        });
    }

    /**
     * 异步post请求
     *
     * @param url
     * @param params
     * @param myInterface
     */
    public void postAsynHttp(final OkHttpInterface myInterface, final String url, final Map<String, Object> params, final Context context) {
        // OkHttpClient mOkHttpClient = new OkHttpClient();

        Log.e(TAG, "////异步post请求" + url);
        FormEncodingBuilder builer = getFormBuilder(params);
        Request request = new Request.Builder().url(url)
//                .addHeader(GlobalConstant.access_token, access_token)
//                .addHeader(GlobalConstant.CONTENT_TYPE, GlobalConstant.APPLICATION_JSON)
                .post(builer.build()).build();
        Call call = mOkHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
//                currentActivity.httpError(url, params);
                ToastUtils.dismissLoadingToast();
                Log.e(TAG, "////异步post请求......onFailure");
                myInterface.setHttpFailure(url, request, e);
            }

            @Override
            public void onResponse(Response response) throws IOException {
                ToastUtils.dismissLoadingToast();
//                currentActivity.httpResponse(url, params, response.body().string())
                String str = response.body().string();

                int code = response.code();
//                code=401;
                Log.e(TAG, "code方法:" + code);
                //成功了会是200，失败了一种会报500

                Log.e(TAG, "utils方法:" + str);

                if (code == 200) {
                    myInterface.setHttpResponse(url, str);
                } else {
                }
//
            }
        });
    }

    private FormEncodingBuilder getFormBuilder(Map<String, Object> params) {
        FormEncodingBuilder builder = new FormEncodingBuilder();
        if (params != null && params.size() > 0) {
            Set<Entry<String, Object>> set = params.entrySet();
            Iterator<Entry<String, Object>> iterator = set.iterator();
            while (iterator.hasNext()) {
                Entry<String, Object> entry = iterator.next();
//                Log.e(TAG,"key:"+entry.getKey()+"    value:"+entry.getValue().toString());
                Object value = entry.getValue();
                if (value != null) {
                    Log.e(TAG, "key:" + entry.getKey() + "    value:" + entry.getValue().toString());
                    builder.add(entry.getKey(), entry.getValue().toString());
                }
            }
        }
        return builder;
    }


    private String getString(Map<String, Object> params) {
//        FormEncodingBuilder builder = new FormEncodingBuilder();
        String str = "";
        if (params != null && params.size() > 0) {
            Set<Entry<String, Object>> set = params.entrySet();
            Iterator<Entry<String, Object>> iterator = set.iterator();
            while (iterator.hasNext()) {
                Entry<String, Object> entry = iterator.next();
//                Log.e(TAG,"key:"+entry.getKey()+"    value:"+entry.getValue().toString());
                Object value = entry.getValue();
                if (value != null) {
                    Log.e(TAG, "key:" + entry.getKey() + "    value:" + entry.getValue().toString());
//                    builder.add(entry.getKey(), entry.getValue().toString());
                    str = str + entry.getKey() + entry.getValue().toString();
                }
            }
        }
        return str;
    }


    /**
     * @param url              请求URL
     * @param callbackListener 请求结果回调监听
     * @描述 Post异步请求(无参)
     * @创建人 jh
     * @创建时间 2016/7/19
     * 默认添加token
     */
    public void post(String url, FSSCallbackListener<Object> callbackListener) {
        Log.e(TAG, "/..........post：" + url);
        FSSHandler handler = new FSSHandler(mContext, callbackListener);
        Map<String, Object> paramMap = new HashMap<String, Object>();
        okHttpPost(url, paramMap, handler);
    }

    /**
     * @param url              请求URL
     * @param paramMap         参数集合
     * @param callbackListener 请求结果回调监听
     * @描述 Post异步请求(有参)
     * @创建人 guoyanlu
     * @创建时间 2016/7/19
     */
    public void post(String url, Map<String, Object> paramMap, FSSCallbackListener<Object> callbackListener, boolean tokenFlag) {
        FSSHandler handler = new FSSHandler(mContext, callbackListener);
        if (tokenFlag) {  //添加token
            okHttpPostToken(url, paramMap, handler);
//            postTokenType(url, paramMap, handler);
        } else {   // 不用添加token
            okHttpPost(url, paramMap, handler);
        }

    }

//    public void post(String url, Map<String, Object> paramMap, FSSCallbackListener<Object> callbackListener) {
//        FSSHandler handler = new FSSHandler(mContext, callbackListener);
//        okHttpPostTokenType(url, paramMap, handler);
//    }

    /**
     * 异步post请求
     *
     * @param url
     * @param params Map<String, Object> params
     *               此方法是post集合中要设置
     *               MediaType.parse("application/json")
     */
    public void postTokenType(final String url, final Map<String, Object> params, FSSCallbackListener<Object> callbackListener) {

        final FSSHandler handler = new FSSHandler(mContext, callbackListener);
        // OkHttpClient mOkHttpClient = new OkHttpClient();
        Log.e(TAG, "////异步post请求" + url);
//        String access_token = ShareUtils.getString(mContext, GlobalConstant.access_token, null);
//        if (TextUtils.isEmpty(access_token)) {
//            access_token = "";
//        }

//        FormEncodingBuilder builer = getFormBuilder(params);
//        String string = getString(params);
        JSONObject object = new JSONObject(params);

//        MediaType mediaType = builer.build().contentType();
//        mediaType.parse("application/json; charset=utf-8");
//        builer.build().contentType().parse("application/json; charset=utf-8");
//         builer.build().contentType().parse("application/json");

//        String s = builer.build().toString();
        Log.e(TAG, "//参数 object" + object.toString());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), object.toString());

        Request request = new Request.Builder().url(url)
//                .addHeader(GlobalConstant.access_token, access_token)
//                .addHeader(GlobalConstant.CONTENT_TYPE, GlobalConstant.APPLICATION_JSON)
                .post(requestBody).build();


        final Call call = mOkHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
//                currentActivity.httpError(url, params);
                ToastUtils.dismissLoadingToast();
                Log.e(TAG, "////异步post请求......onFailure");
//                myInterface.setHttpFailure(url, request, e);

                responseErrorObjct(RespCodeMsgConstant.RESP_CONNECTION_TIMEOUT_MSG, handler);

            }

            @Override
            public void onResponse(Response response) throws IOException {
                ToastUtils.dismissLoadingToast();
//                currentActivity.httpResponse(url, params, response.body().string())
                String str = response.body().string();
                Log.e(TAG, "onResponse方法:" + str);
                int code = response.code();
                Log.e(TAG, "code方法:" + code);
                //成功了会是200，失败了一种会报500

//                Log.e(TAG, "utils方法:" + str);

                if (code == 200) {
//                    myInterface.setHttpResponse(url, str);
                    responseSucessObjct(str, handler);
                } else {

                    if (mContext != null) {
                        responseErrorObjct(str, handler);
                    }

                }
//
            }
        });
    }


    /**
     * 异步post请求
     *
     * @param url
     * @param params Map<String, Object> params
     *               此方法是post集合中要设置
     *               MediaType.parse("application/json")
     */
    public void postTokenType(final String url, final Map<String, Object> params, FSSCallbackListener<Object> callbackListener, boolean flagToken) {

        final FSSHandler handler = new FSSHandler(mContext, callbackListener);
        // OkHttpClient mOkHttpClient = new OkHttpClient();
        Log.e(TAG, "////异步post请求" + url);

//        FormEncodingBuilder builer = getFormBuilder(params);
//        String string = getString(params);
        JSONObject object = new JSONObject(params);

//        MediaType mediaType = builer.build().contentType();
//        mediaType.parse("application/json; charset=utf-8");
//        builer.build().contentType().parse("application/json; charset=utf-8");
//         builer.build().contentType().parse("application/json");

//        String s = builer.build().toString();
        Log.e(TAG, "//参数 object" + object.toString());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), object.toString());
        Request request;
        if (flagToken) {
            request = new Request.Builder().url(url)

                    .post(requestBody).build();
        } else {
            request = new Request.Builder().url(url)
                    .post(requestBody).build();
        }

        final Call call = mOkHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
//                currentActivity.httpError(url, params);
                ToastUtils.dismissLoadingToast();
                Log.e(TAG, "////异步post请求......onFailure");
//                myInterface.setHttpFailure(url, request, e);

                responseErrorObjct(RespCodeMsgConstant.RESP_CONNECTION_TIMEOUT_MSG, handler);

            }

            @Override
            public void onResponse(Response response) throws IOException {
                ToastUtils.dismissLoadingToast();
//                currentActivity.httpResponse(url, params, response.body().string())
                String str = response.body().string();
                Log.e(TAG, "onResponse方法:" + str);
                int code = response.code();
                Log.e(TAG, "code方法:" + code);
                //成功了会是200，失败了一种会报500

                Log.e(TAG, "utils方法:" + str);

                if (code == 200) {
//                    myInterface.setHttpResponse(url, str);
                    responseSucessObjct(str, handler);
                } else {
                    if (mContext != null) {
                        responseErrorObjct(str, handler);
                    }

                }
//
            }
        });
    }


    /**
     * 异步post请求
     *
     * @param url
     * @param str 此方法是post集合中要设置
     *            MediaType.parse("application/json"
     */
    public void postTokenType(final String url, final String str, FSSCallbackListener<Object> callbackListener) {

        final FSSHandler handler = new FSSHandler(mContext, callbackListener);
        // OkHttpClient mOkHttpClient = new OkHttpClient();
        Log.e(TAG, "////异步post请求" + url);

//        JSONObject object = new JSONObject(params);

        Log.e(TAG, "//参数 object" + str);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), str);

        Request request = new Request.Builder().url(url)
                .post(requestBody).build();


        final Call call = mOkHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
//                currentActivity.httpError(url, params);
                ToastUtils.dismissLoadingToast();
                Log.e(TAG, "////异步post请求......onFailure");
//                myInterface.setHttpFailure(url, request, e);
                responseErrorObjct(RespCodeMsgConstant.RESP_CONNECTION_TIMEOUT_MSG, handler);

            }

            @Override
            public void onResponse(Response response) throws IOException {
                ToastUtils.dismissLoadingToast();
//                currentActivity.httpResponse(url, params, response.body().string())
                String str = response.body().string();
                Log.e(TAG, "onResponse方法:" + str);
                int code = response.code();
                Log.e(TAG, "code方法:" + code);
                //成功了会是200，失败了一种会报500
//                Log.e(TAG, "utils方法:" + str);
                if (code == 200) {
//                    myInterface.setHttpResponse(url, str);
                    responseSucessObjct(str, handler);
                } else {
//                    Looper.prepare();
//                    ToastUtils.setToastActivity((Context) myInterface,"网络请求异常，请稍后重试");
//                    Looper.loop();
//                    MyApplication.netWorkErrorTips(context,code+"", (Activity)context);
                    if (mContext != null) {
                        responseErrorObjct(str, handler);
                    }

                }
//
            }
        });
    }


    /**
     * 异步post请求
     *
     * @param url
     * @param params
     */
    public void okHttpPostToken(final String url, final Map<String, Object> params, final Handler handler) {

        // OkHttpClient mOkHttpClient = new OkHttpClient();
        Log.e(TAG, "////异步post请求" + url);
        FormEncodingBuilder builer = getFormBuilder(params);

        Request request = new Request.Builder().url(url)

                .post(builer.build()).build();
        final Call call = mOkHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
//                currentActivity.httpError(url, params);
                ToastUtils.dismissLoadingToast();
                Log.e(TAG, "////异步post请求......onFailure");
//                myInterface.setHttpFailure(url, request, e);
                responseErrorObjct(RespCodeMsgConstant.RESP_CONNECTION_TIMEOUT_MSG, handler);

            }

            @Override
            public void onResponse(Response response) throws IOException {
                ToastUtils.dismissLoadingToast();
//                currentActivity.httpResponse(url, params, response.body().string())
                String str = response.body().string();
                Log.e(TAG, "onResponse方法:" + str);
                int code = response.code();
                Log.e(TAG, "code方法:" + code);
                //成功了会是200，失败了一种会报500

                Log.e(TAG, "utils方法:" + str);

                if (code == 200) {
//                    myInterface.setHttpResponse(url, str);
                    responseSucessObjct(str, handler);
                } else {
//                    Looper.prepare();
//                    ToastUtils.setToastActivity((Context) myInterface,"网络请求异常，请稍后重试");
//                    Looper.loop();
//                    MyApplication.netWorkErrorTips(context,code+"", (Activity)context);
                    if (mContext != null) {
                        responseErrorObjct(str, handler);
                    }

                }
//
            }
        });
    }


    /**
     * 异步post请求
     *
     * @param url
     * @param params
     * @param handler 不加 token
     */
    public void okHttpPost(final String url, final Map<String, Object> params, final Handler handler) {
        // OkHttpClient mOkHttpClient = new OkHttpClient();
        Log.e(TAG, "////异步post请求" + url);
        FormEncodingBuilder builer = getFormBuilder(params);
        Request request = new Request.Builder().url(url)
                .post(builer.build()).build();
        Call call = mOkHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
//                currentActivity.httpError(url, params);
                ToastUtils.dismissLoadingToast();
                Log.e(TAG, "////异步post请求......onFailure");
                String string = request.toString();
                RequestBody body = request.body();
                String stringBoy = body.toString();
                responseErrorObjct(RespCodeMsgConstant.RESP_CONNECTION_TIMEOUT_MSG, handler);
            }

            @Override
            public void onResponse(Response response) throws IOException {
                ToastUtils.dismissLoadingToast();
//                currentActivity.httpResponse(url, params, response.body().string())
                String str = response.body().string();

                int code = response.code();
                Log.e(TAG, "code方法:" + code);
                //成功了会是200，失败了一种会报500

                Log.e(TAG, "utils方法:" + str);

                if (code == 200) {
//                    myInterface.setHttpResponse(url, str);
                    responseSucessObjct(str, handler);
                } else {
//                    Looper.prepare();
//                    ToastUtils.setToastActivity((Context) myInterface,"网络请求异常，请稍后重试");
//                    Looper.loop();
                    if (mContext != null) {
                        responseErrorObjct(str, handler);
                    }

                }
//
            }
        });
    }


    /**
     * get的请求方法
     *
     * @param url
     * @param callbackListener 是否添加token 可以直接拼接在字符串里,也可不加
     */
    public void get(String url, FSSCallbackListener<Object> callbackListener) {
        FSSHandler handler = new FSSHandler(mContext, callbackListener);
//        RequestParams params=new RequestParams();

        okHttGet(url, handler);
    }



    /**
     * @param url
     * @param callbackListener
     * @param tokenFlag        是否添加token
     */
    public void get(String url, FSSCallbackListener<Object> callbackListener, boolean tokenFlag) {
        FSSHandler handler = new FSSHandler(mContext, callbackListener);
        okHttGet(url, handler);

    }

    /**
     * @param url
     * @param params           Map集合，添加参数
     * @param callbackListener
     * @param tokenFlag        是否添加token
     */
    public void get(String url, final Map<String, Object> params, FSSCallbackListener<Object> callbackListener, boolean tokenFlag) {
        final FSSHandler handler = new FSSHandler(mContext, callbackListener);
        Log.e(TAG, "////////异步get请求" + url);

        JSONObject object = new JSONObject(params);
        Log.e(TAG, "//参数 object" + object.toString());

        Request.Builder reqBuild = new Request.Builder();
        HttpUrl.Builder urlBuilder = HttpUrl.parse(url)
                .newBuilder();
//        urlBuilder.addQueryParameter("uid", oauth2AccessToken.getUid());

        for (Entry<String, Object> entry : params.entrySet()) {
//           System.out.println("键 key ："+entry.getKey()+" 值value ："+entry.getValue());
            urlBuilder.addQueryParameter(entry.getKey(), entry.getValue() + "");
        }


        reqBuild.url(urlBuilder.build());
        Request request = reqBuild.build();

        Call call = mOkHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
//                inter.setHttpFailure(url, request, e);
                Log.e(TAG, "////////异步get请求onFailure");
                responseErrorObjct(RespCodeMsgConstant.RESP_CONNECTION_TIMEOUT_MSG, handler);

            }

            @Override
            public void onResponse(final Response response) throws IOException {

                int code = response.code();
                String str = response.body().string();
                Log.e(TAG, "utils方法:" + str);
                if (code == 200) {
//                    inter.setHttpResponse(url, str);
                    responseSucessObjct(str, handler);
                } else {
                    if (mContext != null) {
                        responseErrorObjct(str, handler);
                    }
                }

            }
        });

    }


    /**
     * 异步get请求
     *
     * @param url
     */
    public void okHttGet(final String url, final Handler handler) {

//        Request request = new Request.Builder().addHeader("Authorization", "Bearer " +
// Account.user_token).url(urlString).get().build();

        Log.e(TAG, "////////异步get请求" + url);
//        final Request request = new Request.Builder().url(url).build();

        Request request = new Request.Builder().url(url)
                .get().build();


        Call call = mOkHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
//                inter.setHttpFailure(url, request, e);

                Log.e(TAG, "////////异步get请求onFailure");

                responseErrorObjct(RespCodeMsgConstant.RESP_CONNECTION_TIMEOUT_MSG, handler);



            }

            @Override
            public void onResponse(final Response response) throws IOException {

                int code = response.code();
                String str = response.body().string();
                Log.e(TAG, "utils方法:" + str);
                if (code == 200) {
//                    inter.setHttpResponse(url, str);
                    responseSucessObjct(str, handler);
                } else {
                    if (mContext != null) {
                        responseErrorObjct(str, handler);
                    }
                }

            }
        });
    }


    /**
     * 异步get请求
     * 自定义token
     *
     * @param url 加token
     */
    public void okHttpToken(final String url, final Handler handler, String token) {


    }


    /**
     * @param respObj 检验内容
     * @param handler 回调Handler
     * @描述 响应正确信息
     * @创建人 guoyanlu
     * @创建时间 2016/7/19
     */
    public static void responseSucessObjct(Object respObj, Handler handler) {

//        LogUtilsUtils.e(TAG,"//////回调Handler获取");
        FSSResponse resp = new FSSResponse(HttpConstant.RESPONSE_SUCCESS,
                RespCodeMsgConstant.CODE_SUCCESS_MSG, respObj);
        Message msg = new Message();
        msg.obj = resp;
        handler.sendMessage(msg);
    }

    /**
     * @param errorMsg 错误提示信息
     * @param handler  回调Handler
     * @描述 响应错误信息
     * @创建人 guoyanlu
     * @创建时间 2016/7/19
     */
    public static void responseErrorObjct(String errorMsg, Handler handler) {
        FSSResponse resp = new FSSResponse(HttpConstant.RESPONSE_FAILURE,
                errorMsg);
        Message msg = new Message();
        msg.obj = resp;
        handler.sendMessage(msg);
    }


}
